home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / Amiga-E / E_v3.2a / Src / Utils / Nkript.e < prev    next >
Text File  |  1992-09-02  |  2KB  |  72 lines

  1. /* Nkript.e, very simple file (de)coder
  2.    USAGE: nkript <file>
  3.  
  4.    nkript asks for a 4 letter key, and a 3 letter pincode.
  5.    as nkript uses EOR, you may use this program to code and
  6.    decode. the key and pincode are not stored anywhere, so it
  7.    _relatively_ safe. this has the effect that if you type the
  8.    wrong key, no error is given, but the file is simply
  9.    decoded wrong.
  10.  
  11. */
  12.  
  13. MODULE 'tools/file'
  14.  
  15. ENUM ER_NONE,ER_FILE,ER_MEM,ER_USAGE,ER_OUT,ER_ILLEGAL,ER_NONUM
  16.  
  17. PROC main() HANDLE
  18.   DEF flen,mem=NIL,key,keyadd,file[200]:STRING,p
  19.   WriteF('Nkript (c) 1992 $#%!\n')
  20.   IF StrCmp(arg,'',1) OR StrCmp(arg,'?',2) THEN Raise(ER_USAGE)
  21.   mem,flen:=readfile(arg)
  22.   key:=readpass('key',4,FALSE)
  23.   keyadd:=readpass('pin',3,TRUE) OR 3
  24.   WriteF('Now (de)coding "\s".\n',arg)
  25.   MOVE.L flen,D7
  26.   LSR.L  #2,D7        /* D7 = #of LONGs */
  27.   MOVE.L key,D6
  28.   MOVE.L keyadd,D4
  29.   MOVE.L mem,A0
  30.   loop:
  31.   MOVE.L D4,D5
  32.   SUB.L  D6,D5
  33.   LSL.L  #3,D6        /* random alg.  D6*7+keyadd (11) */
  34.   ADD.L  D5,D6
  35.   EOR.L  D6,(A0)+
  36.   DBRA   D7,loop
  37.   SUB.L  #$10000,D7
  38.   BCC.S  loop        /* DBRA.L emulation */
  39.   p:=InStr(arg,'.',0)
  40.   StrCopy(file,arg,p)
  41.   IF StrCmp(arg+p,'.nkr',ALL)=FALSE THEN StrAdd(file,'.nkr',ALL)
  42.   writefile(file,mem,flen)
  43. EXCEPT DO
  44.   IF mem THEN freefile(mem)
  45.   SELECT exception
  46.     CASE ER_NONE;    WriteF('Done.\n')
  47.     CASE "OPEN";     WriteF('Could not access file "\s" !\n',exceptioninfo)
  48.     CASE "IN";       WriteF('Could not read from file "\s" !\n',exceptioninfo)
  49.     CASE "OUT";      WriteF('Could not write to file "\s" !\n',exceptioninfo)
  50.     CASE "MEM";      WriteF('No memory for loading file!\n')
  51.     CASE ER_USAGE;   WriteF('USAGE: Nkript <file>\n')
  52.     CASE ER_ILLEGAL; WriteF('Wrong #of chars\n')
  53.     CASE ER_NONUM;   WriteF('not a decimal number\n')
  54.   ENDSELECT
  55. ENDPROC
  56.  
  57. PROC readpass(messy,numchars,decflag)
  58.   DEF s[25]:STRING,a,t,n=0,f=1
  59.   WriteF('\s[\d]: ',messy,numchars)
  60.   ReadStr(stdout,s)
  61.   IF EstrLen(s)<>numchars THEN Raise(ER_ILLEGAL)
  62.   IF decflag
  63.     t:=s
  64.     FOR a:=1 TO numchars
  65.       n:=n+(t[]-"0"*f)
  66.       IF (t[]<"0") OR (t[]++>"9") THEN Raise(ER_NONUM)
  67.       f:=f*10
  68.     ENDFOR
  69.     ^s:=n
  70.   ENDIF
  71. ENDPROC ^s
  72.